package com.googlecode.garbagecan.clickstudy.service.customer;

import com.googlecode.garbagecan.clickstudy.entity.customer.Customer;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class CustomerService {

	private static int idx = 0; // Must be first line

	private static CustomerService service = new CustomerService();

	private List<Customer> customers;

	private CustomerService() {
		customers = new ArrayList<Customer>();
		for (int i = 0; i < 15; i++) {
			++idx;
			Customer customer = new Customer();
			customer.setId(idx);
			customer.setName("name" + idx);
			customer.setEmail("name@x.x");
			customer.setInvestments("Bonds");
			customers.add(customer);
		}
	}

	public static CustomerService getInstance() {
		return service;
	}

	public List<Customer> getAllCustomers() {
		return customers;
	}

	public Customer find(int id) {
		for (int i = 0; i < customers.size(); i++) {
			if (id == customers.get(i).getId()) {
				return customers.get(i);
			}
		}
		return null;
	}
	
	public void add(Customer customer) {
		customer.setId(++idx);
		customers.add(customer);
	}

	public void update(Customer customer) {
		for (int i = 0; i < customers.size(); i++) {
			if (customer.getId() == customers.get(i).getId()) {
				customers.set(i, customer);
			}
		}
	}
	
	public void delete(Customer customer) {
		for (int i = 0; i < customers.size(); i++) {
			if (customer.getId() == customers.get(i).getId()) {
				customers.remove(i);
				break;
			}
		}
	}
	
	public void delete(int id) {
		for (int i = 0; i < customers.size(); i++) {
			if (id == customers.get(i).getId()) {
				customers.remove(i);
				break;
			}
		}
	}
	
	public Map<String, String> getInvestmentCatetories() {
		Map<String, String> investments = new LinkedHashMap<String, String>();
		investments.put("Bonds", "Bonds");
		investments.put("Commerical Property", "Commerical Property");
		investments.put("Options", "Options");
		investments.put("Residential Property", "Residential Property");
		investments.put("Stocks", "Stocks");
		return investments;
	}
}
